﻿using System;
using System.Threading.Tasks;
using System.Web.Http;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.Web.Models;
using Microsoft.Owin.Infrastructure;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using LeadThing.AbpZeroTemplate.Authorization;
using LeadThing.AbpZeroTemplate.Authorization.Users;
using LeadThing.AbpZeroTemplate.MultiTenancy;
using LeadThing.AbpZeroTemplate.WebApi.Models;
using System.Web;
using LeadThing.AbpZeroTemplate.Files.Dtos;
using System.IO;
using LeadThing.AbpZeroTemplate.Files;
using Abp.Runtime.Session;
using LeadThing.AbpZeroTemplate.Authorization.Users.Dto;
using LeadThing.AbpZeroTemplateShortMessage;

namespace LeadThing.AbpZeroTemplate.WebApi.Controllers
{
    public class ShortMessageController : AbpZeroTemplateApiControllerBase
    {
        private readonly IUserAppService _userAppService;

        public ShortMessageController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="mobiles">手机号</param>
        /// <returns></returns>
        [HttpPost]
        public AjaxResult SendShortMessage(ShortMessageModel model)
        {
            AjaxResult result = new AjaxResult();
            ErrorInfo error = new ErrorInfo();

            Random rd = new Random();
            string str = "";
            while (str.Length < 4)
            {
                int temp = rd.Next(0, 10);
                if (!str.Contains(temp + ""))
                {
                    str += temp;
                }
            }

            MasShortMessage masShortMessage = new MasShortMessage();

            string content = "欢迎使用平安莒县，验证码为：" + str;
            if (masShortMessage.SendMessage(model.Mobiles, content))
            {
                result.Success = true;
                result.Result = str;
            }
            else
            {
                result.Success = false;
            }

            error.Message = "";
            result.Error = error;
            return result;
        }

    }
}
